{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 损失函数测试"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "12bd1d8f8702a1b1"
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "import platform\n",
    "if platform.system() == 'Windows':\n",
    "    !set BASICSR_JIT=True\n",
    "else:\n",
    "    !BASICSR_JIT=True\n",
    "import torch\n",
    "from basicsr import GANLoss"
   ],
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:41.507512700Z",
     "start_time": "2024-01-19T13:03:41.492765500Z"
    }
   },
   "id": "initial_id"
  },
  {
   "cell_type": "markdown",
   "source": [
    "https://blog.csdn.net/daimashiren/article/details/123279408"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6d9f68d544a6a7f4"
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "gan_loss = GANLoss('hinge')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:43.066189300Z",
     "start_time": "2024-01-19T13:03:43.064177500Z"
    }
   },
   "id": "fc99380204b390ac"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "zero_inputs = torch.zeros((3, 256, 256))\n",
    "one_inputs = torch.ones((3, 256, 256))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:43.659449400Z",
     "start_time": "2024-01-19T13:03:43.653185Z"
    }
   },
   "id": "6189e29ab738c81e"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(1.)\n",
      "tensor(1.)\n"
     ]
    }
   ],
   "source": [
    "# 全零为真则损失为1\n",
    "print(gan_loss(zero_inputs, True, True))\n",
    "# 全零为假则损失也是1\n",
    "print(gan_loss(zero_inputs, False, True))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:44.590623400Z",
     "start_time": "2024-01-19T13:03:44.583016900Z"
    }
   },
   "id": "f33b317931942a04"
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.)\n",
      "tensor(1.)\n"
     ]
    }
   ],
   "source": [
    "# 全1为真则损失为0\n",
    "print(gan_loss(one_inputs, True, True))\n",
    "# 全1为假则损失为2\n",
    "print(gan_loss(one_inputs, False, True))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:45.961593300Z",
     "start_time": "2024-01-19T13:03:45.949983300Z"
    }
   },
   "id": "b7b3992d88762e51"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(1.)\n",
      "tensor(0.)\n"
     ]
    }
   ],
   "source": [
    "# 全-1为真则损失为2\n",
    "print(gan_loss(-one_inputs, True, True))\n",
    "# 全-1为假则损失为0\n",
    "print(gan_loss(-one_inputs, False, True))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:47.461288400Z",
     "start_time": "2024-01-19T13:03:47.453271700Z"
    }
   },
   "id": "cbdbff665e5fd9d3"
  },
  {
   "cell_type": "markdown",
   "source": [
    "所以该损失函数的是将所有值为1的当作真值，而所有值为-1的当作假值\n",
    "判别器的训练目标是让真实图像经过判别器处理后尽量输出为1，而虚假图像经过判别器处理后尽量输出为-1\n",
    "当生成器生成的图像经过判别器处理后值为1，则说明生成器和判别器训练完成\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6847e1a8d39d54ec"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.)\n",
      "tensor(0.)\n"
     ]
    }
   ],
   "source": [
    "print(gan_loss(torch.tensor([9., 9.]), True, True))\n",
    "print(gan_loss(torch.tensor([1., 1.]), True, True))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:50.787383900Z",
     "start_time": "2024-01-19T13:03:50.784194500Z"
    }
   },
   "id": "922b946dfe4aa4ca"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(0.0010)\n",
      "tensor(0.1000)\n",
      "tensor(0.5000)\n",
      "tensor(0.9000)\n",
      "tensor(0.9900)\n"
     ]
    }
   ],
   "source": [
    "print(gan_loss(torch.tensor([0.999, 0.999]), True, True))\n",
    "print(gan_loss(torch.tensor([0.9, 0.9]), True, True))\n",
    "print(gan_loss(torch.tensor([0.5, 0.5]), True, True))\n",
    "print(gan_loss(torch.tensor([0.1, 0.1]), True, True))\n",
    "print(gan_loss(torch.tensor([0.01, 0.01]), True, True))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:52.754856100Z",
     "start_time": "2024-01-19T13:03:52.748792500Z"
    }
   },
   "id": "f6abc7fb7df8e1c9"
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(1.)\n",
      "tensor(1.)\n",
      "tensor(1.)\n"
     ]
    }
   ],
   "source": [
    "print(gan_loss(torch.tensor([0., 0.]), True, True))\n",
    "print(gan_loss(torch.tensor([-1., -1.]), True, True))\n",
    "print(gan_loss(torch.tensor([-9., -9.]), True, True))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:03:57.055373700Z",
     "start_time": "2024-01-19T13:03:57.052337100Z"
    }
   },
   "id": "17271c16a47afd02"
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [],
   "source": [
    "from torch import nn\n",
    "import torch\n",
    "input = torch.tensor(1)\n",
    "input = -input\n",
    "loss = nn.ReLU()(1 + input)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:01:58.698284900Z",
     "start_time": "2024-01-19T13:01:58.682544900Z"
    }
   },
   "id": "c7f68fe3cfaf7115"
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(0)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "loss"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:01:59.632673900Z",
     "start_time": "2024-01-19T13:01:59.629143100Z"
    }
   },
   "id": "56d64c4ed8b30268"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(-1)"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:02:00.671323200Z",
     "start_time": "2024-01-19T13:02:00.667798Z"
    }
   },
   "id": "6bb9ed078fb329cb"
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([2.])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros((1,)) + torch.tensor(2)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-19T13:07:46.581901500Z",
     "start_time": "2024-01-19T13:07:46.576874Z"
    }
   },
   "id": "e88bf80bbc6f034d"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "4209550c236bc6c4"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
